<!DOCTYPE html>
<html lang="en" xmlns:v-on="http://www.w3.org/1999/xhtml" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../../js/vue/vue.js"></script>
    <script src="../../js/jquery/jquery.js"></script>
</head>
<body>
    <!-- 模板 -->
    <div id="app">
        <form id="formId">
            <input type="hidden" name="id" v-bind:value="emps.id">
            姓名：<input type="text" name="name" v-bind:value="emps.name"><br/>
            年龄：<input type="text" name="age" v-bind:value="emps.age"><br/>
            <input type="button" value="提交" v-on:click="submitForm()">
        </form>
    </div>

    <script>
        //-------- vue 数据 -----------
        var vue = new Vue({
            el:"#app",
            data : {
              emps:{}
            },
            methods:{
                submitForm:function () {
                    // alert(1);
                    var param = $("#formId").serialize();
                    $.ajax({
                        url : 'http://127.0.0.1:8080/employees/update',
                        type : 'PUT',
                        data: param,
                        success:function (data) {
                            if (data.code == 200) {
                                window.location.href = 'list.html';
                            }else {
                                alert('操作失败')
                            }
                        },
                    })
                }
            },

            mounted : function(id) {
                var params = getParams();
                $.get('http://127.0.0.1:8080/employees/selectById', {id:params.id}, function (data) {
                    console.log(data);
                    vue.emps = data;
                })
            }
        });

        //http://localhost/views/demo/edit.html?id=21&name=dafei&age=18
        //获取url上的请求参数
        function getParams() {
            //获取问号及问号后面的内容
            //?id=21&name=dafei&age=18
            var url = window.location.search;
            var params = {};
            if (url.indexOf("?") != -1) {
                //截取问号后面的内容,再使用&分割多个属性
                //[id=21,name=dafei,age=18]
                var arr = url.substr(1).split("&");
                for (var i = 0; i < arr.length; i++) {
                    //使用=分割为keyvalue
                    //arr[0]---id=21---> [id, 21]
                    //arr[1]---name=dafei---> [name, dafei]
                    //arr[2]---age=18---> [age, 18]
                    var keyValue = arr[i].split("=");
                    //params[id]= 21----->param.id = 21
                    //params[name]= dafei----->param.name = dafei
                    //params[age]= 18----->param.age = 18
                    params[keyValue[0]] = keyValue[1];
                }
            }
            return params;
        }
    </script>
</body>
</html>